
! Constructs the asset grids for conditional value functions
module grids

use params
use states
use wage

implicit none

real(prec), private :: const, lower, upper
integer, private :: i, j, t, w

contains

	! ***********************************************
	subroutine assetbounds

	do t=nt,1,-1
		if (t.eq.nt) then 
			minincp(t) = incomep(nwp,nt)
			mininck(t) = incomek(nw,nt)
			maxincp(t) = incomep(1,nt)
			maxinck(t) = incomek(1,nt)
		else
			minincp(t) = (minincp(t+1)/r) + incomep(nwp,t)
			mininck(t) = (mininck(t+1)/r) + incomek(nw,t)
			maxincp(t) = (maxincp(t+1)/r) + incomep(1,t)
			maxinck(t) = (maxinck(t+1)/r) + incomek(1,t)
		endif

		do w = 1,nw
			if (t.eq.nt) then
				incleftk(w,t) = incomek(w,nt)
			else 
				incleftk(w,t) = (incleftk(w,t+1)/r) + incomek(w,t)
			endif
		enddo        
		do w = 1,nwp
			if (t.eq.nt) then
				incleftp(w,t) = incomep(w,nt)
			else 
				incleftp(w,t) = (incleftp(w,t+1)/r) + incomep(w,t)
			endif
		enddo       

		alowp(t) = 0.d0
		alowk(t) = 0.d0
		
	enddo
	
	do t = 1,nt
        if (t.eq.1) then
            ahighp(t)= incomep(1,1)
            ahighk(t)= incomek(1,1)
        else
            ahighp(t)= ahighp(t-1)*r+incomep(1,1)
            ahighk(t)= ahighk(t-1)*r+incomek(1,1)
        endif
	enddo

	end subroutine assetbounds
	! ***********************************************
	
	! ***********************************************
	subroutine assetgrid
	
	assetp(1,age) = alowp(age) + minass      
	assetk(1,age) = alowk(age) + minass      
	const = (1000000 - assetp(1,age))/dble(na-1)
 	do i=2,na
		assetp(i,age) = assetp(1,age) + const*dble(i-1)
	enddo
	const = (1000000 - assetk(1,age))/dble(na-1)
 	do i=2,na
		assetk(i,age) = assetk(1,age) + const*dble(i-1)
	enddo    

    achoice(1) = 0d0
    const = (assetp(na,age) - achoice(1))/dble(nachoice-1)
    do i=2,nachoice
	  achoice(i) = achoice(1) + const*dble(i-1)
    end do
	
	end subroutine assetgrid
	! ***********************************************	

end module grids
